home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / utils / soundex.el.z / soundex.el
Encoding:
Text File  |  1998-05-21  |  2.7 KB  |  77 lines

  1. ;;; soundex.el -- implement Soundex algorithm
  2.  
  3. ;; Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Christian Plaunt <chris@bliss.berkeley.edu>
  6. ;; Maintainer: FSF 
  7. ;; Keywords: matching
  8. ;; Created: Sat May 15 14:48:18 1993
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  25. ;; 02111-1307, USA.
  26.  
  27. ;;; Synched up with: FSF 19.34.
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; The Soundex algorithm maps English words into representations of
  32. ;; how they sound.  Words with vaguely similar sound map to the same string.
  33.  
  34. ;;; Code: 
  35.  
  36. (defvar soundex-alist
  37.   '((?B . "1") (?F . "1") (?P . "1") (?V . "1")
  38.     (?C . "2") (?G . "2") (?J . "2") (?K . "2") (?Q . "2") (?S . "2")
  39.     (?X . "2") (?Z . "2") (?D . "3") (?T . "3") (?L . "4") (?M . "5")
  40.     (?N . "5") (?R . "6"))
  41.   "Alist of chars-to-key-code for building Soundex keys.")
  42.  
  43. (defun soundex (word)
  44.   "Return a Soundex key for WORD.
  45. Implemented as described in:
  46. Knuth, Donald E. \"The Art of Computer Programming, Vol. 3: Sorting
  47. and Searching\", Addison-Wesley (1973), pp. 391-392."
  48.   (let* ((word (upcase word)) (length (length word))
  49.      (code (cdr (assq (aref word 0) soundex-alist)))
  50.      (key (substring word 0 1)) (index 1) (prev-code code))
  51.     ;; once we have a four char key, we're done
  52.     (while (and (> 4 (length key)) (< index length))
  53.       ;; look up the code for each letter in word at index
  54.       (setq code (cdr (assq (aref word index) soundex-alist))
  55.         index (1+ index)
  56.         ;; append code to key unless the same codes belong to
  57.         ;; adjacent letters in the original string
  58.         key (concat key (if (or (null code) (string= code prev-code))
  59.                 ()
  60.                   code))
  61.         prev-code code))
  62.     ;; return a key that is 4 chars long and padded by "0"s if needed
  63.     (if (> 4 (length key))
  64.     (substring (concat key "000") 0 4)
  65.       key)))
  66.  
  67. ;(defvar soundex-test
  68. ;  '("Euler" "Gauss" "Hilbert" "Knuth" "Lloyd" "Lukasiewicz"
  69. ;    "Ellery" "Ghosh" "Heilbronn" "Kant" "Ladd" "Lissajous")
  70. ;  "\n  Knuth's names to demonstrate the Soundex algorithm.")
  71. ;
  72. ;(mapcar 'soundex soundex-test)
  73. ;("E460" "G200" "H416" "K530" "L300" "L222"
  74. ; "E460" "G200" "H416" "K530" "L300" "L222")
  75.  
  76. ;; soundex.el ends here
  77.